/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.map;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

import twaver.Element;
import twaver.ElementAttribute;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.VisibleFilter;
import twaver.chart.BarChart;
import twaver.chart.PieChart;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;
import twaver.table.TPropertySheet;

public class MapInfoAttachment extends ComponentAttachment {
	
	private TDataBox chartBox = new TDataBox();
	private PieChart pieChart = new PieChart(chartBox);
	private BarChart barChart = new BarChart(chartBox);

	private Node male = new Node();
	private Node female = new Node();
	private Node employed = new Node();
	private Node unemploy = new Node();

	public MapInfoAttachment(String name, ElementUI ui) {
		super(name, ui);
		initGUI();
		initChartBox();
		initSheet();
		setChartValue();
	}

	private TPropertySheet sheet = new TPropertySheet() {
		protected JTableHeader createDefaultTableHeader() {
			JTableHeader tableHeader = new JTableHeader(columnModel) {
				public void paintComponent(Graphics g) {
					Graphics2D g2 = (Graphics2D) g;
					Rectangle bounds = new Rectangle(0, 0, this.getWidth(), this.getHeight());
					g2.setPaint(TWaverUtil.createGradientPaint(TWaverConst.GRADIENT_LINE_N, bounds, new Color(0, 0, 0, 100), new Color(99, 130, 191, 200)));
					g2.fillRect(0, 0, this.getWidth(), this.getHeight());
					super.paintComponent(g);
				}
			};
			tableHeader.setFont(new Font("Dialog", Font.BOLD, 11));
			tableHeader.setBackground(new Color(0, 0, 0, 0));
			return tableHeader;
		}

		public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
			Component prepareRenderer = super.prepareRenderer(renderer, row, column);
			prepareRenderer.setFont(new Font("Dialog", Font.PLAIN, 10));
			if (row % 2 == 0) {
				prepareRenderer.setBackground(new Color(192, 192, 192, 200));
			} else {
				prepareRenderer.setBackground(new Color(120, 180, 230, 200));
			}
			return prepareRenderer;
		}
	};

	private void initSheet() {
		List list = new ArrayList();
		list.add(createAttribute("LAND_KM", "LAND_KM"));
		list.add(createAttribute("WATER_KM", "WATER_KM"));
		list.add(createAttribute("PERSONS", "PERSONS"));
		list.add(createAttribute("FAMILIES", "FAMILIES"));
		list.add(createAttribute("HOUSHOLD", "HOUSHOLD"));
		list.add(createAttribute("WORKERS", "WORKERS"));
		sheet.registerElementClassAttributes(Node.class, list);
		sheet.setOpaque(false);
		sheet.setIndentOpaque(false);
		sheet.setMode(TPropertySheet.VIEW_AS_FLAT_LIST);
	}

	private ElementAttribute createAttribute(String key, String displayName) {
		ElementAttribute attribute = new ElementAttribute();
		attribute.setClientPropertyKey(key);
		attribute.setDisplayName(displayName);
		return attribute;
	}

	private void initChartBox() {
		male.setName("MALE");
		male.putClientProperty("type", "bar");
		male.putChartColor(new Color(0, 100, 0));
		chartBox.addElement(male);

		female.setName("FEMALE");
		female.putClientProperty("type", "bar");
		female.putChartColor(new Color(250, 100, 0));
		chartBox.addElement(female);

		employed.setName("EMPLOYED");
		employed.putChartColor(new Color(0, 255, 0));

		employed.putClientProperty("type", "pie");
		chartBox.addElement(employed);

		unemploy.setName("UNEMPLOY");
		unemploy.putChartColor(new Color(255, 0, 0));
		unemploy.putClientProperty("type", "pie");
		chartBox.addElement(unemploy);

		pieChart.setOpaque(false);
		pieChart.setStartAngle(-100);
		pieChart.setLegendFont(new Font("Dialog",Font.BOLD,10));
		pieChart.set3D(true);
		pieChart.setValueTextPercent(true);
		pieChart.setShadowOffset(15);

		barChart.setOpaque(false);
		barChart.setYScaleTextColor(new Color(250, 100, 50));
		barChart.setShadowOffset(2);
		barChart.setValueTextVisible(true);
		barChart.setLegendFont(new Font("Dialog",Font.BOLD,10));
		barChart.setYScaleTextVisible(true);
		barChart.setYScaleTextFormat(NumberFormat.getIntegerInstance());
		barChart.getChartPane().setOpaque(false);

		pieChart.addVisibleFilter(new VisibleFilter() {
			public boolean isVisible(Element element) {
				if (element.getClientProperty("type").toString().equals("pie")) {
					return true;
				}
				return false;
			}
		});
		barChart.addVisibleFilter(new VisibleFilter() {
			public boolean isVisible(Element element) {
				if (element.getClientProperty("type").toString().equals("bar")) {
					return true;
				}
				return false;
			}
		});
	}

	private void initGUI() {
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null,
								element.getClientProperty("STATE_NAME").toString(),
								TitledBorder.LEADING,
								TitledBorder.DEFAULT_POSITION,
								TUIManager.getDefaultItalicBoldFont(),
								new Color(0,100,0)));
		panel.setOpaque(false);
		panel.setLayout(new GridLayout(3, 1, 5, 5));
		JScrollPane sheetPanel = new JScrollPane(sheet);
		sheetPanel.getViewport().setOpaque(false);
		sheetPanel.setOpaque(false);
		panel.add(sheetPanel);
		panel.add(pieChart);
		panel.add(barChart);
		pieChart.setOpaque(false);
		barChart.setShadowOffset(0);

		this.setComponent(panel);
		this.setPosition(TWaverConst.POSITION_CENTER);
		this.setBodyVisible(true);
		
		this.setBodyGradient(false);
		this.setBodyColor(new Color(230, 230, 230, 160));
		this.setBorderColor(Color.DARK_GRAY);
		this.setBorderVisible(true);
		this.setStyle(TWaverConst.ATTACHMENT_STYLE_BUBBLE);
		this.setTail(30);
		this.setArc(10);
		this.setWidth(190);
		this.setHeight(380);
		this.setDirection(((MapNode) element).getAttachmentDirection());
	}
	
	private Element createSheetElement(){
		Node node = new Node();
		node.putClientProperty("LAND_KM", element.getClientProperty("LAND_KM"));
		node.putClientProperty("WATER_KM", element.getClientProperty("WATER_KM"));
		node.putClientProperty("PERSONS", element.getClientProperty("PERSONS"));
		node.putClientProperty("FAMILIES", element.getClientProperty("FAMILIES"));
		node.putClientProperty("HOUSHOLD", element.getClientProperty("HOUSHOLD"));
		node.putClientProperty("WORKERS", element.getClientProperty("WORKERS"));
		return node;
	}

	private void setChartValue() {
		sheet.setElement(createSheetElement());
		double value = Double.parseDouble(element.getClientProperty("MALE").toString());
		male.putChartValue(value);
		value = Double.parseDouble(element.getClientProperty("FEMALE").toString());
		female.putChartValue(value);
		value = Double.parseDouble(element.getClientProperty("EMPLOYED").toString());
		employed.putChartValue(value);
		value = Double.parseDouble(element.getClientProperty("UNEMPLOY").toString());
		unemploy.putChartValue(value);
	}
}